// Two function need to be implemented , GameMap_handler & GameMap_handler_configMode
// used GM Variables
// ds_mapJumpOver : for identify the whether current is the jumpping first time.
// ds_lastVisitedPositionList : for record the last position has been hunted,the hunted record will be refreshed in one hour.
// ds_huntRuleCurrent : current hunt rule object .

function GameMap_handler(){
    // no hunt rules, no hunt
    if(GM_safeVariables["ds_huntRuleCurrent"] != null && GM_safeVariables["ds_huntRuleCurrent"] != ''){
        if(GM_safeVariables["ds_mapJumpOver"] == null
        || GM_safeVariables["ds_mapJumpOver"] == ''){
            //calculate jump target postions
            var PI = 3.1415926;
            var isFixRadius = CONFIG.fix_radius;
            var huntRadius = CONFIG.hunt_radius;
            //huntRadius = huntRadius*4;//convert hunt_radius from screen to coordinate system.
            var currentHuntRule = GM_safeVariables["ds_huntRuleCurrent"];
            //{id:"1111",heroName:"苦托斯",radius:2,fixRadius:false,target:"wild/devil/wheel",attackType:"attack/collect"}
            currentHuntRule = eval('('+currentHuntRule+')');
            isFixRadius = currentHuntRule.fixRadius;
            huntRadius = currentHuntRule.radius;
            var strHeroName = '';
            for(var m=0;m<currentHuntRule.heros.length;m++){
                var objHero = currentHuntRule.heros[m];
                strHeroName += objHero.name + ',';
            }
            log("正在按照规则的设定,为<strong>"+strHeroName+"</strong>跳转到"+currentHuntRule.radius.toString()+"格左右的地区.");
            var curX = parseInt($x('//input[@name="xp"]')[0].value);
            var curY = parseInt($x('//input[@name="yp"]')[0].value);
            var randomX,randomY,tarX,tarY;
            if(isFixRadius){ //fix radius logic
                var angle = Math.random()*2*PI;
                randomX = parseInt(Math.cos(angle)*huntRadius);
                randomY = parseInt(Math.sin(angle)*huntRadius);
            }else{
                var angle = Math.random()*2*PI;
                huntRadius = Math.random()*huntRadius;
                randomX = parseInt(Math.cos(angle)*huntRadius);
                randomY = parseInt(Math.sin(angle)*huntRadius);
            }
            tarX = curX + randomX;
            tarY = curY + randomY;
            //jump to target positions and set ds_mapJumpOver
            GM_safeVariables["ds_mapJumpOver"] = 'YES';
            clickLink("/GameMap?xp=" + tarX + "&yp=" + tarY);
        }else{
            //pick up a random place to hunt or attack.follow the primary order.
            //demon > demon city > wild 
            var link = "";
            var targets = [];
            var attackWild = CONFIG.attack_wild;
            var attackDevil = CONFIG.attack_evilCity;
            var attackWheel = CONFIG.attack_wheel;
            var currentHuntRule = GM_safeVariables["ds_huntRuleCurrent"];
            //{id:"1111",heroName:"苦托斯",radius:2,fixRadius:false,target:"wild/devil/wheel",attackType:"attack/collect"}
            currentHuntRule = eval('('+currentHuntRule+')');
            switch(currentHuntRule.target){
                case 'wild':
                    attackWild = true;
                    attackDevil = attackWheel = false;
                break;
                case 'devil':
                    attackDevil = true;
                    attackWild = attackWheel = false;
                break;
                case 'wheel':
                    attackWheel = true;
                    attackDevil = attackWild = false;
                break;
            }
            var strHeroName = '';
            for(var m=0;m<currentHuntRule.heros.length;m++){
                var objHero = currentHuntRule.heros[m];
                strHeroName += objHero.name + ',';
            }
            log("正在按照规则的设定,为<strong>"+strHeroName+"</strong>选择规则预定的地点.");
            //wild place
            if(attackWild){
                var random = Math.random() * 100;
                var direction = "c4";
                //这段是玩家海拉斯特贡献的代码,特此感谢
                if(random >= 0 && random <= 25){
                    direction = "c2";//forest
                }else if (random > 25 && random <= 50){
                    direction = "c3";//country
                }else if (random > 50 && random <= 75){
                    direction = "c4";//marsh
                }else if (random > 75 && random <= 100){
                    direction = "c5";//plain
                }
                targets = $x("//img[@src='/sys/images/c/"+direction+".gif']");
            }
            //devil city
            if(attackDevil){
                if($x("//img[@src='/sys/images/c/c6.gif']").length != 0)
                targets = $x("//img[@src='/sys/images/c/c6.gif']");
            }
            //unvisited wheel 
            if(attackWheel){
                if($x("//img[@src='/sys/images/c/c11.gif']").length != 0
                &&$x("//img[@src='/sys/images/c/c11.gif']")[0].parentNode.childNodes.length < 2){
                    targets = $x("//img[@src='/sys/images/c/c11.gif']");
                }
                if($x("//img[@src='/sys/images/c/c12.gif']").length != 0
                &&$x("//img[@src='/sys/images/c/c12.gif']")[0].parentNode.childNodes.length < 2){
                    targets = $x("//img[@src='/sys/images/c/c12.gif']");
                }
            }
            if(targets.length != 0){
                var xyPosition = "";
                var showCityInfo = function(pos){
                    xyPosition = pos;
                };
                //随机对象抽取代码,由玩家海拉斯特贡献
                var iTarget = Math.round(Math.random()* targets.length - 0.5);
                eval(targets[iTarget].parentNode.getAttribute('onmouseover'));
                link = targets[iTarget].parentNode.getAttribute('href');
                //cache for target list
                var targetList = null;
                if(GM_safeVariables['ds_lastVisitedPositionList'] != null && GM_safeVariables['ds_lastVisitedPositionList'] != ""){
                    targetList = eval("(" + GM_safeVariables['ds_lastVisitedPositionList'] + ")");
                    if(targetList.ts != (new Date().getHours().toString())){
                        targetList = {ts:new Date().getHours().toString(),targets:[]};
                    }
                }else{
                    targetList = {ts:new Date().getHours().toString(),targets:[]};
                }      
                if(targetList.targets.exists(xyPosition) == -1){   
                    delete GM_safeVariables["ds_mapJumpOver"];
                    targetList.targets.push(xyPosition);
                    GM_safeVariables['ds_lastVisitedPositionList'] = uneval(targetList);
                    clickLink(link);
                    return;
                }else{
                    log("短时间内再次选中攻击过的目标,取消攻击重新在地图中搜索...");
                    delete GM_safeVariables["ds_mapJumpOver"];
                    GM_safeVariables['ds_lastVisitedPositionList'] = uneval(targetList);
                    clickLink("/GameMap");
                    return;
                }      
            }else{
                log("地图上无法找到任何能够攻击的目标，继续在地图中搜索...");
                clickLink("/GameMap");
                delete GM_safeVariables["ds_mapJumpOver"];
            }
        }
    }else{
        log("您没有制定任何的狩猎规则,将自动跳转回首页...");
        clickLink('/mindex');
        return;
    }
    
}

function GameMap_handler_configMode(){

}